<?php
namespace App\Models\Authority\Method;

use App\Models\Authority\Interfaces\AuthorityInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class crmUser implements AuthorityInterface
{
    public function Login(&$code, $uID)
    {
        $user = DB::connection('jmft_crm')->table('crm_users')
            ->where('userid', $uID)->first();
        return $user;
    }
    public function Reg(&$code, $uInfo,  $rqt)
    {
        $data = [
            'userid'=>$uInfo->usersid,
            'username'=>$uInfo->username,
            'realname'=>$uInfo->real_name,
            'phone'=>$uInfo->phone,
            'is_service'=>$rqt['is_service'],
            'create_time'=>$uInfo->created_at
        ];
        DB::connection("jmft_crm")->beginTransaction();
        if ($user = DB::connection('jmft_crm')->table('crm_users')->insert($data)) {
            if ($rqt['is_service'] == 0) {
                $s_data = [
                    'id'=>getuuID(),
                    'user_id'=>$uInfo->usersid,
                    'name'=>$rqt['name'],
                    'account'=>$rqt['account'],
                    'servicenum'=>$rqt['servicenum'],
                    'money'=>$rqt['money'],
                    'username'=>$rqt['username'],
                    'phone'=>$rqt['phone'],
                    'fax'=>$rqt['fax'],
                    'province'=>$rqt['province'],
                    'city'=>$rqt['city'],
                    'area'=>$rqt['area'],
                    'address'=>$rqt['address'],
                    'created_at'=>date('Y-m-d H:i:s'),
                    'updated_at'=>date('Y-m-d H:i:s'),
                    'status'=>0,
                ];
                if ($service = DB::connection('jmft_crm')->table('crm_service')->insert($s_data)) {
                    $roles_id = DB::connection('jmft_crm')->table('crm_roles')->where('name', '=', 'serviceinit')->select(['id'])->first();
                    if (DB::connection('jmft_crm')->table('crm_role_user')->insert(['user_id'=>$uInfo->usersid, 'role_id'=>$roles_id->id])) {
                        DB::connection("jmft_crm")->commit();
                        return $service;
                    } else {
                        DB::connection("jmft_crm")->rollBack();
                    }
                } else {
                    DB::connection("jmft_crm")->rollBack();
                }
            } else {
                DB::connection("jmft_crm")->commit();
                return $user;
            } 
        } else {
            DB::connection("jmft_crm")->rollBack();
        }
    }
    public function Logout(&$code, $uID)
    {

    }
    public function Edit(&$code, $uInfo,  $rqt)
    {
        $data = [];
        $c_data = [];
        if (isset($rqt['username'])) {
            $data['username'] = $rqt['username'];
            $c_data['username'] = $rqt['username'];
        }
        if (isset($rqt['head_pic'])) {
            $data['head_pic'] = $rqt['head_pic'];
        }
        if (isset($rqt['real_name'])) {
            $data['real_name'] = $rqt['real_name'];
            $c_data['realname'] = $rqt['real_name'];
        }
        if (isset($rqt['sex'])) {
            $data['sex'] = $rqt['sex'];
        }
        if (isset($rqt['job'])) {
            $data['job'] = $rqt['job'];
        }
        if (isset($rqt['cardID'])) {
            $data['cardID'] = $rqt['cardID'];
        }
        if (isset($rqt['status']) && $rqt['status'] != '') {
            $data['status'] = $rqt['status'];
            $c_data['status'] = $rqt['status'];
        }
        if (DB::connection('jmft_uc')->table('uc_users')->where(['usersid'=>$rqt['usersid']])->update($data)) {
            DB::connection("jmft_crm")->beginTransaction();
            if ($result = DB::connection('jmft_crm')->table('crm_users')->where(['userid'=>$rqt['usersid']])->update($c_data)) {
                DB::connection("jmft_crm")->commit();
                return $result;
            } else {
                DB::connection("jmft_crm")->rollBack();
            }
        }
    }
}
